package com.baibei.ebec.sdk;

import android.util.Log;

import com.baibei.sdk.Empty;
import com.blankj.utilcode.utils.LogUtils;

import java.io.IOException;

import retrofit2.Call;

/**
 * Refresh Token Requester
 * Created by ChenRui on 2017/8/29 0029 15:20.
 */
public class RefreshTokenRequester {

    ITokenApi mTokenApi;

    boolean mRefreshTokenIsRunning;
    boolean mRefreshAppKeyIsRunning;

    public RefreshTokenRequester() {
    }

    public void refresh(String refreshToken) throws IOException {
        if (mTokenApi == null) {
            mTokenApi = ApiFactory.getInstance().getTokenApi();
        }


        LogUtils.w("rae-token", "-----------------------------------发 TOKEN请求------------------------------------------");
        if (mRefreshTokenIsRunning) {
            LogUtils.e("rae-token", "已经有在刷新TOKEN请求啦");
            // 等待请求执行完成
            long start = System.currentTimeMillis();
            while (true) {
                // 等待超时处理
                if (System.currentTimeMillis() - start >= 10000) break;
                if (!mRefreshTokenIsRunning) break;
            }

            LogUtils.i("rae-token", "~~~~~ 刷新TOKEN请求完成了 ~~~~~");
        }

        Call<TokenInfo> call = mTokenApi.refreshToken(refreshToken);
        mRefreshTokenIsRunning = true;
        try {
            call.execute();
        } catch (IOException e) {
            mRefreshTokenIsRunning = false;
        } finally {
            mRefreshTokenIsRunning = false;
        }

        Log.w("rae-token", "-----------------------------------结束TOKEN请求------------------------------------------");
    }

    public void refreshAppKey() throws IOException {
    }


}
